package lg.minecraft.plugin.cobblemoncore.item.builder;

import com.cobblemon.mod.common.pokemon.Species;
import lg.minecraft.plugin.cobblemoncore.util.BukkitNmsConverter;
import lg.minecraft.plugin.library.common.PlaceholderReplacer;
import lg.minecraft.plugin.library.item.ItemProperties;
import lg.minecraft.plugin.library.item.builder.AbstractItemBuilder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class PokemonPlaceholderItemBuilder extends AbstractItemBuilder {

    public PokemonPlaceholderItemBuilder(ItemProperties properties) {
        super(properties);
    }

    @Override
    protected ItemStack build(Map<String, String> map, Object... objects) {
        return buildBasicItem(properties, map, (Species) objects[0]);
    }

    public ItemStack build(Species species, Map<String, String> map) {
        return build(map, species);
    }

    public static ItemStack buildBasicItem(ItemProperties properties, Map<String, String> map, Species species) {
        ItemStack stack = BukkitNmsConverter.getPokemonItem(species);
        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(PlaceholderReplacer.replaceInString(properties.getName(), map));
            meta.setLore(PlaceholderReplacer.replaceInList(properties.getLore(), map));
            properties.getEnchantments().forEach((enchantedEntry) -> meta.addEnchant(enchantedEntry.getEnchantment(), enchantedEntry.getLevel(), true));
            properties.getItemFlags().forEach(meta::addItemFlags);
            stack.setItemMeta(meta);
        }

        return stack;
    }

}
